package com.hinnotek.warehouse.ui.fragment

import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.View
import androidx.fragment.app.FragmentTransaction
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.databinding.FragmentMainBinding
import com.lee.common.base.BaseFragment
import com.lee.common.event.GO_TO_OIL_PAGE
import com.lee.common.event.MessageEvent
import com.lee.common.utils.FRAGMENT_MAIN
import com.lee.common.utils.MyActivityManager
import com.lee.common.utils.ToastUtils
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @ProjectName: YunAnXin
 * @Author: RICO
 * @Time: 2020/11/16 16:19
 * @Description:
 */
@Route(path = FRAGMENT_MAIN)
class MainFragment : BaseFragment<FragmentMainBinding>() {

    private var homeFragment: HomeFragment? = null
    private var myFragment: MyFragment? = null


    override fun getLayoutId(): Int {
        return R.layout.fragment_main
    }

    override fun initData() {

    }


    override fun initView(view: View) {
        initBnvView()
        showFragment(FRAGMENT_HOME)
        setImageSize(30.0f, 22.0f)
        registerEvent(this@MainFragment)
        super.initView(view)
    }

    override fun onDestroy() {
        registerEvent(this@MainFragment)
        super.onDestroy()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun changePage(msg: MessageEvent) {
        if (msg.status == GO_TO_OIL_PAGE) {
            showFragment(FRAGMENT_HOME)
            mBinding.bnvBar.selectedItemId = R.id.menu_item_main
        }
    }

    /**
     * 初始化事件
     */
    private fun initBnvView() {
        mBinding.bnvBar.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.menu_item_main -> showFragment(FRAGMENT_HOME)
                R.id.menu_item_my -> showFragment(FRAGMENT_MINE)

            }
            true
        }
    }

    /**
     * 显示当前Fragment
     *
     * @param index
     */
    private fun showFragment(index: Int) {
        val ft = childFragmentManager.beginTransaction()
        hideFragment(ft)
        when (index) {
            //首页
            FRAGMENT_HOME ->
                if (homeFragment == null) {
                    homeFragment = HomeFragment()
                    ft.add(R.id.fll_main, homeFragment!!, HomeFragment::class.java.name)
                } else {
                    ft.show(homeFragment!!)
                }


            //我的
            FRAGMENT_MINE -> if (myFragment == null) {
                myFragment = MyFragment()
                ft.add(R.id.fll_main, myFragment!!, HomeFragment::class.java.name)
            } else {
                ft.show(myFragment!!)
            }

        }
        ft.commit()
    }

    /**
     * 隐藏全部Fragment
     *
     * @param ft
     */
    private fun hideFragment(ft: FragmentTransaction) { // 如果不为空，就先隐藏起来
        if (homeFragment != null) {
            ft.hide(homeFragment!!)
        }
        if (myFragment != null) {
            ft.hide(myFragment!!)
        }
    }

    companion object {
        private const val FRAGMENT_HOME = 0
        private const val FRAGMENT_MINE = 1
    }


    /**
     * 设置MenuView图片尺寸
     */
    private fun setImageSize(width: Float, height: Float) {
        val menuView = mBinding.bnvBar.getChildAt(0) as BottomNavigationMenuView
        try {
            for (i in 0 until menuView.childCount) {
                val item = menuView.getChildAt(i) as BottomNavigationItemView
                val imageView = item.findViewById<View>(com.google.android.material.R.id.icon)
                val displayMetrics = resources.displayMetrics

                imageView.layoutParams.width = getDimensionSize(displayMetrics, width)
                imageView.layoutParams.height = getDimensionSize(displayMetrics, height)
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun getDimensionSize(displayMetrics: DisplayMetrics, size: Float): Int {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, displayMetrics).toInt()
    }

    private var touchTime: Long = 0

    override fun onBackPressed() {
        val time = 2000L
        if (System.currentTimeMillis() - touchTime < time) {
            MyActivityManager.instance.exitApp(requireContext())
        } else {
            touchTime = System.currentTimeMillis()
            ToastUtils.showLong("再按一次退出")
            return
        }
        super.onBackPressed()
    }



}